<template>
  <div class="spot-check-page">
    <div class="page-header">
      <h2>
        <el-icon><CircleCheck /></el-icon>
        设备点检管理
      </h2>
      <el-button type="primary" @click="handleCreateCheck">
        <el-icon><Plus /></el-icon>
        新建点检
      </el-button>
    </div>

    <el-tabs v-model="activeTab">
      <!-- 点检计划 -->
      <el-tab-pane label="点检计划" name="plan">
        <el-card>
          <el-table :data="checkPlans" stripe>
            <el-table-column prop="name" label="点检项目" min-width="150" />
            <el-table-column prop="equipmentName" label="设备名称" width="150" />
            <el-table-column prop="cycle" label="点检周期" width="100" />
            <el-table-column prop="checker" label="点检人" width="100" />
            <el-table-column prop="nextDate" label="下次点检" width="120" />
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="row.status === '正常' ? 'success' : 'warning'">
                  {{ row.status }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
              <template #default="{ row }">
                <el-button type="primary" size="small" @click="handleExecuteCheck(row)">
                  执行点检
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>

      <!-- 点检记录 -->
      <el-tab-pane label="点检记录" name="record">
        <el-card>
          <el-table :data="checkRecords" stripe>
            <el-table-column prop="code" label="点检单号" width="150" />
            <el-table-column prop="equipmentName" label="设备名称" width="150" />
            <el-table-column prop="checkItems" label="点检项数" width="100" />
            <el-table-column prop="checker" label="点检人" width="100" />
            <el-table-column prop="checkTime" label="点检时间" width="160" />
            <el-table-column prop="result" label="点检结果" width="100">
              <template #default="{ row }">
                <el-tag :type="row.result === '合格' ? 'success' : 'danger'">
                  {{ row.result }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
              <template #default>
                <el-button type="primary" size="small" text>
                  查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>
    </el-tabs>

    <!-- 点检对话框 -->
    <el-dialog v-model="showDialog" title="设备点检" width="900px">
      <el-form label-width="100px">
        <el-form-item label="设备">
          <strong>数控车床 EQ-CNC-001</strong>
        </el-form-item>
        <el-form-item label="点检项目">
          <el-table :data="checkItems" border>
            <el-table-column prop="item" label="点检项目" width="200" />
            <el-table-column prop="standard" label="标准" width="200" />
            <el-table-column label="点检结果" width="150">
              <template #default="scope">
                <el-select v-model="scope.row.result" size="small">
                  <el-option label="合格" value="pass" />
                  <el-option label="不合格" value="fail" />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="备注">
              <template #default="scope">
                <el-input v-model="scope.row.remark" size="small" />
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="handleSubmitCheck">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { CircleCheck, Plus } from '@element-plus/icons-vue'

const activeTab = ref('plan')
const showDialog = ref(false)

const checkPlans = ref([
  {
    id: 1,
    name: '每日点检',
    equipmentName: '数控车床',
    cycle: '每日',
    checker: '张三',
    nextDate: '2024-10-15',
    status: '正常'
  },
  {
    id: 2,
    name: '每周点检',
    equipmentName: '加工中心',
    cycle: '每周',
    checker: '李四',
    nextDate: '2024-10-18',
    status: '正常'
  }
])

const checkRecords = ref([
  {
    id: 1,
    code: 'CHK-2024-001',
    equipmentName: '数控车床',
    checkItems: 12,
    checker: '张三',
    checkTime: '2024-10-14 08:30',
    result: '合格'
  },
  {
    id: 2,
    code: 'CHK-2024-002',
    equipmentName: '加工中心',
    checkItems: 15,
    checker: '李四',
    checkTime: '2024-10-13 09:00',
    result: '合格'
  }
])

const checkItems = ref([
  { item: '润滑油油位', standard: '在上下限之间', result: 'pass', remark: '' },
  { item: '设备清洁度', standard: '无油污和杂物', result: 'pass', remark: '' },
  { item: '安全防护', standard: '完好无损', result: 'pass', remark: '' }
])

const handleCreateCheck = () => {
  ElMessage.info('新建点检功能开发中')
}

const handleExecuteCheck = (row) => {
  showDialog.value = true
}

const handleSubmitCheck = () => {
  showDialog.value = false
  ElMessage.success('点检记录提交成功')
}
</script>

<style scoped>
.spot-check-page {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}
</style>
